import { RequestHandler } from 'express'
import { failure, success } from '@/utils/response'
import brandService from '@/services/brandService'

const getBrands: RequestHandler = async function (req, res) {
  try {
    const data = await brandService.getBrands(req.query)
    success(res, '查询品牌列表成功。', data)
  } catch (error) {
    failure(res, error as Error, '查询品牌列表失败：')
  }
}

const addBrand: RequestHandler = async function (req, res) {
  try {
    const data = await brandService.addBrand(req.body)
    success(res, '新增品牌成功。', data, 201)
  } catch (error) {
    failure(res, error as Error, '新增品牌失败：')
  }
}

const getBrandInfo: RequestHandler = async function (req, res) {
  try {
    const data = await brandService.getBrandInfo(+req.params.id)
    success(res, '查询品牌成功。', data)
  } catch (error) {
    failure(res, error as Error, '查询品牌失败：')
  }
}

const updateBrand: RequestHandler = async function (req, res) {
  try {
    await brandService.updateBrand(+req.params.id, req.body)
    success(res, '更新品牌成功。')
  } catch (error) {
    failure(res, error as Error, '更新品牌失败：')
  }
}

const deleteBrand: RequestHandler = async function (req, res) {
  try {
    await brandService.deleteBrand(+req.params.id)
    success(res, '删除品牌成功。')
  } catch (error) {
    failure(res, error as Error, '删除品牌失败：')
  }
}

export default {
  getBrands,
  addBrand,
  getBrandInfo,
  updateBrand,
  deleteBrand,
}
